package com.hdoit.apollo.web.shop.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.viewmodel.shop.CustomerSearch;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月26日 11:53:47
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/customer/")
public class CustomerController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/customer/";
    }

    @Autowired
    private ICustomerService customerService;

    private final String indexViewName="index";
    private final String projectIndexViewName="project/index";
    private final String outputViewName="output";
    private final String projectOutputViewName="project/output";
    private final String detailedViewName="addCustomersDetailed";
    private final String editViewName="detailed";
    private final String saveViewName="save";
    private final String savesViewName="saves";
    private final String deleteViewName = "delete";
    private final String customerListViewName = "customerList";

    @RequestMapping(value = customerListViewName, method = { RequestMethod.GET }, produces = "text/html;charset=UTF-8")
   	public @ResponseBody String supplierList(PagerModel pagerModel,CustomerSearch search) {

    	 List<Customer> customers=customerService.queryListMP(search, null);
   		
   		return JsonUtils.writeValueAsString(customers);
   	}
    
    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,CustomerSearch search,boolean ajaxPost){
    	
        pagerModel.setTotalAmount(customerService.queryCountMP(search));
        List<Customer> customers= customerService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("customers", customers);
        model.addAttribute("customerSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }
    
    @RequestMapping(value = projectIndexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String projectIndex(Model model,PagerModel pagerModel,CustomerSearch search,boolean ajaxPost){
    	
        pagerModel.setTotalAmount(customerService.queryCountMP(search));
        List<Customer> customers= customerService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("customers", customers);
        model.addAttribute("customerSearch", search);

        if(ajaxPost){
            return view(projectOutputViewName);
        }

        return view(projectIndexViewName);
    }
   
    @ResponseBody
    @RequestMapping(value = savesViewName, method = { RequestMethod.POST },produces = "text/json;charset=UTF-8")
    public String saves(@RequestBody List<Customer> customers){
        
    	
    	for (Customer customer : customers) {
            customer.setCustomerId(CommonUtils.getKeyId());
            customer.setCreateDate(DateUtils.getCurrent());
            customer.setDeleteMark(YesOrNoInteger.no);
            customer.setCreateUserId(getLoginModel().getLoginId());
            customer.setCreateUserName(getLoginModel().getLoginName());
            customer.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            customer.setShopId(shop.getShopId());
           
        }

        if(customerService.saveCustomers(customers)){
        	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }else{
        	return new JsonActionResult(ManageContainer.ERROR).toJson();
        }

    }
    
    @RequestMapping(value = editViewName, method = { RequestMethod.GET})
    public String detailed(Model model,String key){
        Customer customer=new Customer();
        if(!StringUtils.isEmpty(key)){
            customer=customerService.queryModel(key);
        }
        model.addAttribute("customer", customer);
        return view(editViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String customerDetailed(Model model,String key){
        Customer customer=new Customer();
        if(!StringUtils.isEmpty(key)){
            customer=customerService.queryModel(key);
        }
        model.addAttribute("customer", customer);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Customer customer){
        if(StringUtils.isEmpty(customer.getCustomerId())){
            customer.setCustomerId(CommonUtils.getKeyId());
            customer.setCreateDate(DateUtils.getCurrent());
            customer.setDeleteMark(YesOrNoInteger.no);
            customer.setCreateUserId(getLoginModel().getLoginId());
            customer.setCreateUserName(getLoginModel().getLoginName());
            customer.setModelStatus(ModelStatus.add);
        }else{
            customer.setModifyDate(DateUtils.getCurrent());
            customer.setModifyUserId(getLoginModel().getLoginId());
            customer.setModifyUserName(getLoginModel().getLoginName());
            customer.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(customerService.applyChange(customer)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("customer", customer);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(customerService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}